/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;

import java.util.Iterator;

import alloy.semantic.LocalScope;
import alloy.transform.LeafIdMap;
import alloy.type.RelationType;
import alloy.type.UnionType;
import alloy.util.Dbg;

/**
 * @link Iterator} for iterating over the {@link Variable}s
 * declared in a {@link Node} that is a {@link VarCreator}.  Has
 * convenience methods for accessing the current variable's
 * {@link RelationType}, {@link LeafId} and {@link MultiplicityExpr multiplicity}.  
 * <p>
 * {@link DeclIter} abstracts away some of the complexities of accessing
 * the list of variables declared in a {@link VarCreator}.  Things would be more
 * uniform if  {@link Variable}s were {@link Leaf}s with {@link LeafId}s, but
 * changing the current setup would be error-prone.
 */
public class DeclIter implements Iterator {
    /** Local scope -- used to get the {@link RelationType} of the current {@link Variable}. */
    private final LocalScope _localScope;
    /** {@link LeafId} map -- used to get the {@link LeafId} of the current {@link Variable}. */
    private final LeafIdMap _leafIdMap;
    /** Iterator over the {@link Decl}s in the {@link VarCreator}. */
    private final Iterator _declIter;
    /** Iterator over the {@link Variable}s in the
	{@link #_declIter current} {@link Decl}. */
    private Iterator _varIter;
    /** The last {@link Variable} returned by {@link #next}. */
    private Variable _curVar;
    /** The name of {@link #_curVar} */
    private String _curVarName;
    /** The current {@link Decl}aration -- used to get the {@link MultiplicityExpr multiplicity}
	of the current variable. */
    private Decl _curDecl;

      /////////////
     // Methods //
    /////////////

    /** Create a {@link DeclIter} iterator for iterating over the quantified {@link Variable}s
	declared in the given {@link VarCreator}. */
    public DeclIter(VarCreator node_) {
	_localScope = node_.getLocalScope();
	_leafIdMap = node_.getLeafIdMap();
	Decls decls = node_.getDecls();
	_declIter = decls.getDeclIter();
	_varIter = null;
    }

    /** Whether {@link #next} can return another {@link Variable} declared in the
	{@link VarCreator}. */
    public boolean hasNext() { return _declIter.hasNext() || _varIter.hasNext(); }

    /** Returns the next {@link Variable}. */
    public Object next() {
	Dbg.chk(hasNext());
	if (_varIter == null || !_varIter.hasNext()) {
	    _curDecl = (Decl)_declIter.next();
	    _varIter = _curDecl.getVariables().getVariableIter();
	}
	_curVar = (Variable)_varIter.next();
	_curVarName = _curVar.nodeString();
	return _curVar;
    }  // next()

    /** Return the next {@link Variable}, cast to the correct type. */
    public Variable nextVar() { return (Variable)next(); }

    /** Return the {@link Variable} returned by the last call to {@link #next}. */
    public Variable getVariable() { return _curVar; }

    /** Return the {@link MultiplicityExpr} giving the multiplicity of the last
	{@link Variable} returned by {@link #next}. */
    public MultiplicityExpr getMultiplicity() { return _curDecl.getExpr(); }

    /** Return the {@link UnionType type} of the last returned {@link Variable}. */
    public UnionType getType() { return _localScope.lookupType(_curVarName); }

    /** Return the {@link LeafId} of the last returned {@link Variable}. */
    public LeafId getLeafId() { return _leafIdMap.lookupLeafId(_curVarName); }

    /** Remove not supported -- this is a read-only iterator */
    public void remove() throws UnsupportedOperationException
    { throw new UnsupportedOperationException("DeclIter.remove()"); }
}  // class DeclIter
